<template>
  <div class="data-catalog-page">
    <el-card>
      <template #header>
        <div class="header-bar">
          <span>数据目录管理</span>
          <div>
            <el-input v-model="search.name" placeholder="目录名称" size="small" style="width: 200px; margin-right: 10px;" clearable />
            <el-select v-model="search.engine" placeholder="所属引擎" size="small" style="width: 150px; margin-right: 10px;" clearable>
              <el-option v-for="e in engineOptions" :key="e" :label="e" :value="e" />
            </el-select>
            <el-button type="primary" size="small" @click="filterList">搜索</el-button>
          </div>
        </div>
      </template>

      <el-table :data="filteredCatalogs" border stripe style="width: 100%">
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="name" label="目录名称" />
        <el-table-column prop="engine" label="所属引擎" width="140" />
        <el-table-column prop="created" label="创建时间" width="160" />
        <el-table-column prop="remark" label="备注" />
        <el-table-column label="操作" width="220">
          <template #default="{ row }">
            <el-button type="primary" link @click="viewCatalog(row)">查看</el-button>
            <el-button type="success" link @click="collectCatalog(row)">采集</el-button>
            <el-button type="danger" link @click="confirmDelete(row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <el-dialog v-model="viewDialogVisible" title="目录详情" width="500px">
      <el-descriptions :title="viewTarget?.name" column="1" border>
        <el-descriptions-item label="所属引擎">{{ viewTarget?.engine }}</el-descriptions-item>
        <el-descriptions-item label="创建时间">{{ viewTarget?.created }}</el-descriptions-item>
        <el-descriptions-item label="备注">{{ viewTarget?.remark }}</el-descriptions-item>
      </el-descriptions>
      <template #footer>
        <el-button @click="viewDialogVisible = false">关闭</el-button>
      </template>
    </el-dialog>

    <el-dialog v-model="collectDialogVisible" title="采集结果" width="400px">
      <div>目录“{{ collectTarget?.name }}”采集成功，已生成采集记录。</div>
      <template #footer>
        <el-button type="primary" @click="collectDialogVisible = false">确定</el-button>
      </template>
    </el-dialog>

    <el-dialog v-model="deleteDialogVisible" title="删除确认" width="400px">
      <div>确定要删除目录“{{ deleteTarget?.name }}”吗？</div>
      <template #footer>
        <el-button @click="deleteDialogVisible = false">取消</el-button>
        <el-button type="danger" @click="deleteCatalog">确认删除</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, computed } from 'vue'
import { ElMessage } from 'element-plus'

const search = ref({ name: '', engine: '' })
const engineOptions = ["MySQL", "PostgreSQL", "Oracle", "SQLServer", "SQLite"]

const viewDialogVisible = ref(false)
const viewTarget = ref(null)

const collectDialogVisible = ref(false)
const collectTarget = ref(null)

const deleteDialogVisible = ref(false)
const deleteTarget = ref(null)

const catalogs = ref([
  { id: 1, name: '用户目录A', engine: 'MySQL', created: '2024-01-01', remark: '核心用户数据' },
  { id: 2, name: '订单目录B', engine: 'PostgreSQL', created: '2024-01-02', remark: '电商订单信息' },
  { id: 3, name: '传感器目录C', engine: 'Oracle', created: '2024-01-03', remark: '物联网数据目录' },
  { id: 4, name: '日志目录D', engine: 'SQLServer', created: '2024-01-04', remark: '系统运行日志' },
  { id: 5, name: '配置目录E', engine: 'SQLite', created: '2024-01-05', remark: '系统配置元数据' },
  { id: 6, name: '设备目录F', engine: 'MySQL', created: '2024-01-06', remark: '设备信息库' },
  { id: 7, name: '营销目录G', engine: 'Oracle', created: '2024-01-07', remark: '营销数据内容' },
  { id: 8, name: '库存目录H', engine: 'PostgreSQL', created: '2024-01-08', remark: '库存管理' },
  { id: 9, name: '视频目录I', engine: 'SQLite', created: '2024-01-09', remark: '监控视频目录' },
  { id: 10, name: '财务目录J', engine: 'SQLServer', created: '2024-01-10', remark: '财务数据中心' }
])

const filteredCatalogs = computed(() => {
  return catalogs.value.filter(item => {
    return (
      (!search.value.name || item.name.includes(search.value.name)) &&
      (!search.value.engine || item.engine === search.value.engine)
    )
  })
})

function filterList() {
  // 由 computed 直接响应，无需操作
}

function viewCatalog(row) {
  viewTarget.value = row
  viewDialogVisible.value = true
}

function collectCatalog(row) {
  collectTarget.value = row
  collectDialogVisible.value = true
}

function confirmDelete(row) {
  deleteTarget.value = row
  deleteDialogVisible.value = true
}

function deleteCatalog() {
  catalogs.value = catalogs.value.filter(item => item.id !== deleteTarget.value.id)
  ElMessage.success('删除成功')
  deleteDialogVisible.value = false
}
</script>

<style scoped>
.header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
}
</style>
